<template>
  <div>
    <el-dialog
      :model-value="props.dialogVisible"
      :title="props.formStatus === 1 ? '编辑角色' : '新增角色'"
      width="500px"
      :close-on-click-modal="false"
      :before-close="handleClose">
      <el-form ref="roleFormRef" :model="roleForm" :rules="roleFormRules" label-width="100px" @submit.prevent>
        <el-form-item label="角色名称：" prop="roleName">
          <el-input v-model="roleForm.roleName" clearable placeholder="请输入角色名称"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="handleSubmit(roleFormRef)">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="RoleModifyForm">
import { ref, reactive, watch, ComponentInternalInstance, getCurrentInstance } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { roleInsert, roleUpdate } from '@/api/role-manage'

const { proxy } = getCurrentInstance() as ComponentInternalInstance

const emit = defineEmits(['submit', 'update:dialogVisible'])
const props = defineProps({
  dialogVisible: {
    type: Boolean,
    default: false
  },
  formStatus: {
    type: Number,
    default: 0
  },
  formData: {
    type: Object,
    default: {}
  }
})

const roleForm = reactive({
  roleId: -1,
  roleName: '',
})
const roleFormRef = ref<FormInstance>()
const roleFormRules = reactive<FormRules>({
  roleName: [
    { required: true, message: '请输入角色名称！' }
  ],
})

watch(() => props.dialogVisible, async (newVal) => {
  if (newVal) {
    if (props.formStatus === 1) {
      roleForm.roleId = props.formData.roleId
      roleForm.roleName = props.formData.roleName
    }
  }
})

// 重置表单
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

// 关闭弹窗
const handleClose = () => {
  resetForm(roleFormRef.value)
  proxy?.$emit('update:dialogVisible', false)
}

// 提交表单
const handleSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid, fields) => {
    if (valid) {
      if (props.formStatus === 0) {
        let { roleId, ...params } = roleForm
        await roleInsert(params).then((res: any) => {
          if (res.code === 0) {
            ElMessage.success(`新增成功！`)
            emit('submit')
            handleClose()
          } else {
            ElMessage.error(`${res.msg}`)
          }
        }).catch(err => {
          console.log(err)
        })
      } else {
        let params = Object.assign({}, roleForm)
        await roleUpdate(params).then((res: any) => {
          if (res.code === 0) {
            ElMessage.success(`编辑成功！`)
            emit('submit')
            handleClose()
          } else {
            ElMessage.error(`${res.msg}`)
          }
        }).catch(err => {
          console.log(err)
        })
      }
    }
  })
}
</script>

<style lang="stylus" scoped>

</style>
